package com.cloudoer.blog.controller;

import com.cloudoer.blog.base.BaseController;
import com.cloudoer.blog.model.Photo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/photo")
public class PhotoController extends BaseController {

    @RequestMapping(value = "/index")
    public String index (){
        return "photo/index";
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    @ResponseBody
    public Photo getById (String id){
        return photoService.getById(id);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map getByPage (String albumId, @RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<Photo> list = photoService.getByPage(albumId, (pno-1)*psize, psize);
        return result(list, photoService.getCount(), "");
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    @ResponseBody
    public Map getAll (String albumId){
        List<Photo> list = photoService.getAll(albumId);
        return result(list, list.size(), "");
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Map upload (String albumId,MultipartFile imgs) throws ServletException, IOException {
        photoService.add(imgs, albumId);
        return result(SUCCESS, "上传成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map delete (String id){
        photoService.delete(id);
        return result(SUCCESS, "删除成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map update (Photo photo){
        photoService.update(photo);
        return result(SUCCESS, "操作成功");
    }
}
